#include "mainwindow.h"
#include <QDir>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
    //
    prevPlayAction = new QAction(tr("上一首"));
    playAction = new QAction(tr("播放"));
    pausePlayAction = new QAction(tr("暂停"));
    nextPlayAction = new QAction(tr("下一首"));
    increaseVolume = new QAction(tr("音量+"));
    decreaseVolume = new QAction(tr("音量-"));
    aboutAction = new QAction(tr("关于"));
    quitAction = new QAction(tr("退出"));


    QWidget *widget = new QWidget;
    QVBoxLayout *mainLayout = new QVBoxLayout;
    layout1 = new QHBoxLayout;
    iconLabel = new QLabel(tr("图标"));
    settingButton = new QPushButton(tr("设置"));
    minButton = new QPushButton(tr("最小化"));
    maxButton = new QPushButton(tr("最大化"));
    closeButton = new QPushButton(tr("关闭"));

    layout1->addWidget(iconLabel);
    layout1->addWidget(settingButton);
    layout1->addWidget(minButton);
    layout1->addWidget(maxButton);
    layout1->addWidget(closeButton);

    layout2 = new QHBoxLayout;
    songNameList = new QListWidget;
    songWordList = new QListWidget;

    layout2->addWidget(songNameList);
    layout2->addWidget(songWordList);

    layout3 = new QHBoxLayout;
    processSlider = new QSlider;
    processSlider->setOrientation(Qt::Horizontal);
    processLabel = new QLabel(tr("播放进度"));
    layout3->addWidget(processSlider);
    layout3->addWidget(processLabel);

    layout4 = new QHBoxLayout;
    headIcon = new QLabel(tr("头像"));
    songName = new QLabel(tr("歌名"));
    prevButton = new QPushButton(tr("上一曲"));
    playButton = new QPushButton(tr("播放"));
    playlist = new QMediaPlaylist;
    player = new QMediaPlayer;
    playlist->setPlaybackMode(QMediaPlaylist::Loop);  // 播放模式
    player->setPlaylist(playlist); // 设置播放列表
    player->setVolume(50);

    nextButton = new QPushButton(tr("下一曲"));
    volumeLabel = new QLabel(tr("音量"));
    volumeSlider = new QSlider;
    volumeSlider->setOrientation(Qt::Horizontal);
    volumeSlider->setMinimum(volumeMin); // 最大值
    volumeSlider->setMaximum(volumeMax); // 最大值
    volumeSlider->setSingleStep(volumeStep); // 步长
    //playModeButton = new QPushButton(tr("播放模式"));
    playModeBox = new QComboBox(this);
    playModeBox->addItem(tr("顺序播放"));
    playModeBox->addItem(tr("随机播放"));
    playModeBox->addItem(tr("单曲循环"));


    layout4->addWidget(headIcon);
    layout4->addWidget(songName);
    layout4->addWidget(prevButton);
    layout4->addWidget(playButton);
    layout4->addWidget(nextButton);
    layout4->addWidget(volumeLabel);
    layout4->addWidget(volumeSlider);
    layout4->addWidget(playModeBox);


    mainLayout->addLayout(layout1);
    mainLayout->addLayout(layout2);
    mainLayout->addLayout(layout3);
    mainLayout->addLayout(layout4);
    mainLayout->addStretch(1);

    initSongList();
    createSystemTray();

    widget->setLayout(mainLayout);
    setCentralWidget(widget);

    connect(maxButton, &QPushButton::clicked, this, [=]() {
        this->setWindowState(Qt::WindowMaximized);
    });
    connect(minButton, &QPushButton::clicked, this, [=]() {
        this->setWindowState(Qt::WindowMinimized);
    });
    connect(closeButton, &QPushButton::clicked, this, &MainWindow::quitSlot);

    connect(processSlider, SIGNAL(sliderMoved(int)), this, SLOT(setPosition(int)));
    connect(player, SIGNAL(positionChanged(qint64)), this, SLOT(positionChanged(qint64)));
    connect(player, SIGNAL(durationChanged(qint64)), this, SLOT(durationChanged(qint64)));

    connect(playButton, &QPushButton::clicked, this, &MainWindow::playSlot);
    connect(volumeSlider, SIGNAL(valueChanged(int)), this, SLOT(volumeSlot(int)));

    connect(prevButton, &QPushButton::clicked, this, &MainWindow::playPrev);
    connect(nextButton, &QPushButton::clicked, this, &MainWindow::playNext);

    connect(songNameList, SIGNAL(currentRowChanged(int)), this, SLOT(playTo(int)));
    connect(playModeBox, SIGNAL(currentIndexChanged(int)), this, SLOT(setPlayMode(int))); // 改变模式

    connect(quitAction, &QAction::triggered, this, &MainWindow::quitSlot);
}

MainWindow::~MainWindow()
{

}

void MainWindow::quitSlot() {
    qApp->quit();
}

void MainWindow::initSongList() {
    QDir dir(path);
    QStringList nameFilter;

    QStringList files = dir.entryList(nameFilter, QDir::Files|QDir::Readable, QDir::Name);

    //qDebug() << files;
    for(int i = 0; i < files.size(); i++) {
        songNameList->addItem(files[i]);

        QString p = QString(tr("%1%2").arg(path).arg(files[i]));

        playlist->addMedia(QUrl::fromLocalFile(p));
    }
}

void MainWindow::playSlot() {
    if (playButton->text() == tr("播放")) {
        player->play();
        playButton->setText(tr("暂停"));
    } else {
        player->pause();
        playButton->setText(tr("播放"));
    }
}

void MainWindow::volumeSlot(int value) {
    player->setVolume(value);
    volumeLabel->setText(tr("%1").arg(value));
}

void MainWindow::setPosition(int value) {
    // 拖动进度条
    player->setPosition(value);
}

void MainWindow::positionChanged(qint64 value) {
    // 播放进度
    processSlider->setValue(value);
}

void MainWindow::durationChanged(qint64 value) {
    processSlider->setRange(0, value);
}

void MainWindow::playPrev() {
    int currentIndex = playlist->currentIndex();;
    if (--currentIndex < 0)
        currentIndex = playlist->mediaCount() - 1;

    playlist->setCurrentIndex(currentIndex);
    player->play();
}

void MainWindow::playNext() {
    int currentIndex = playlist->currentIndex();;
    if (++currentIndex == playlist->mediaCount())
        currentIndex = 0;

    playlist->setCurrentIndex(currentIndex);
    player->play();
}

void MainWindow::playTo(int value) {
    playlist->setCurrentIndex(value);
    player->play();
}

void MainWindow::setPlayMode(int value) {
    if (value == 0) {
        // 顺序播放
        playlist->setPlaybackMode(QMediaPlaylist::Sequential);
    } else if (value == 1) {
        // 随机播放
        playlist->setPlaybackMode(QMediaPlaylist::Random);
    } else if (value == 2) {
        // 单曲循环
        playlist->setPlaybackMode(QMediaPlaylist::CurrentItemInLoop);
    }
}

void MainWindow::createSystemTray()
{
    trayIcon = new QSystemTrayIcon(this);
    trayIcon->setIcon(QIcon(tr(":/img/icon.ico")));
    trayIcon->setToolTip(tr("酷我 - 音乐播放器"));

    QMenu *trayContextMenu = new QMenu;
    trayContextMenu->addAction(prevPlayAction);
    trayContextMenu->addAction(playAction);
    trayContextMenu->addAction(pausePlayAction);
    trayContextMenu->addAction(nextPlayAction);
    trayContextMenu->addSeparator();
    trayContextMenu->addAction(increaseVolume);
    trayContextMenu->addAction(decreaseVolume);
    trayContextMenu->addSeparator();
    trayContextMenu->addAction(aboutAction);
    trayContextMenu->addAction(quitAction);

    trayIcon->setContextMenu(trayContextMenu);

    trayIcon->show();
    connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
    this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
}

void MainWindow::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
    switch (reason) {
    case QSystemTrayIcon::DoubleClick:
    case QSystemTrayIcon::Trigger:
    if (this->isVisible() == true) {
        ;
    } else {
       this->show();
       this->activateWindow();
    }
        break;
        default:
        break;
    }
}
